/*
 * BookDetailsComments.fx
 * Author Yu Min-ting
 * Created on 2009-7-20, 14:29:34
 */

package com.sjtu.fx.comments;

import javafx.scene.*;
import com.sjtu.fx.tools.Panel;
import javafx.scene.control.Label;

import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;

import javafx.ext.swing.SwingComboBox;
import javafx.ext.swing.SwingComboBoxItem;
import javafx.scene.control.TextBox;
import javafx.scene.control.Button;

import javafx.io.http.HttpRequest;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.json.JSONObject;
import com.sjtu.fx.tools.SwingTable;
import com.sjtu.fx.tools.SwingTable.*;



import javafx.scene.layout.VBox;


import javafx.scene.control.Hyperlink;

import com.sjtu.fx.tools.UIAlert;


public class Comment{
    public var userName:String;
    public var content:String;
    public var date:String;
    public var score:Integer;
    }
public class BookDetailsComments extends CustomNode{
     var panel:Panel;
     public var comments:Comment[];
     public var selectionRow: Integer;
     var commentDetails:CommentDetails;
     var selection:String on replace{
         if(selection == "Specific Person"){
             personName.disable = false;
         }else personName.disable = true;

     }

    var commentTable = SwingTable{
        columns: [
                    TableColumn {
                        text: "Published Date"
                    },
                    TableColumn {
                        text: "User Name"
                    },
                    TableColumn {
                        text: "Content"
                    },
                    TableColumn {
                        text: "Book Score"
                    },
                ]
        rows: bind for (u in comments)
                    TableRow{
                        cells:[
                            TableCell {
                                text:bind u.date
                            },
                            TableCell {
                                text:bind u.userName
                            },
                            TableCell {
                                text:bind u.content
                            },
                            TableCell {
                                text:bind u.score.toString()
                            },

                        ]
                    }
        selection: bind selectionRow with inverse

    }

     var sort = Label {
             text: "Sort By"
             font: Font {size:12 name: "Verdana Bold"}
             textFill: Color.rgb(11,51,60)
         }
     var choices : SwingComboBox = SwingComboBox {
         text: bind selection with inverse
         items: [
             SwingComboBoxItem {
                 text: "Comment Date(descending)"
                 selected: true
             },
             SwingComboBoxItem { text: "Comment Date(ascending)" },
             SwingComboBoxItem { text: "Comment Score(descending)" },
             SwingComboBoxItem { text: "Comment Score(ascending)"},
             SwingComboBoxItem { text: "Specific Person"},
         ]
       
      }

     var personName = TextBox {
             promptText: "Input the person name"
             columns: 12
             selectOnFocus: true
             disable:true
         }

     var button = Button {
             text: "Sort"
             action: function() {
                 // to do 
             }
         }

     var alert = UIAlert {
         translateX:75
         translateY:200
         title:"ERROR"
         content:"Please input entire information"
         width:280
         height:120
         visible:false
         opacity:0.9
     }

    var details = Hyperlink {
            text: "See Details>>"
            action: function() {
              commentDetails.date = comments[selectionRow].date;
              commentDetails.description = comments[selectionRow].content;
              commentDetails.name = comments[selectionRow].userName;
              commentDetails.score = comments[selectionRow].score.toString();
              commentDetails.visible = true;
            }
        }
     override protected function create() : Node{
          Group{
            content:[
                panel = Panel {
                    width:    780
                    height:   305
                    title:    "Book Comments"
                    translateX: 0
                    translateY: 0
                },
                HBox{
                    spacing:50
                    translateX:30 translateY:50
                    content:[commentTable,
                        VBox{
                            spacing: 20
                            translateY:20
                            content:[details, sort,choices,personName,button]
                        }
                   ]
                },
                commentDetails = CommentDetails{visible:false}
                alert
                ]
          }
     }

function getComments(){
    HttpRequest {
        method:HttpRequest.POST
        location:"http://localhost:8764/JavaFXBack/GetComment"
       onOutput: function(os:OutputStream) {
            try {
                var json:JSONObject = JSONObject{};
   /*             json.put("userId",userId);
                json.put("bookId", bookId);
                json.put("comment",comment);
                json.put("score",score);
     */           var temp:String = "obj={json.toString()}";
         //       System.out.println(temp);
                os.write(temp.getBytes());
                os.flush();
            } finally {
                os.close();
            }
        }
        onInput: function(is: InputStream) {
            try {
                def data:DataInputStream = new DataInputStream(is);
                var result:String = data.readLine();
                alert.title = "Result";
                alert.content = result;
                alert.visible = true;
                //Alert.inform(result);
            } finally {
                is.close();
            }
        }
    }.start();
}

}
